<?php
namespace app\admin\controller;
use app\admin\logic\ClassLearnLogic;

/**
 * 学习api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/10/22
 * Time: 15:08
 */
class ClassLearn extends Base {

    public $clid = 1;

    public function __construct(){
        parent::__construct();
        $this->cl_logic = new ClassLearnLogic();
    }

    /**
     * 获取学习概述
     * @return \think\response\Json
     */
    public function ajax_get_cl_overview(){
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        $maps = [];
        $maps[] = ['id','=',$this->clid];
        $detail = model('class_learn')->get_detail($maps,'id,active_name,active_title,active_object,active_unit,start_time,end_time');

        //总参与人数
        $maps = [];
        $total_user = model('class_learn_ci')->get_count($maps,'id','openid');
        //总学习次数
        $maps = [];
        $total_ci = model('class_learn_ci')->get_count($maps,'id');

        $data['detail'] = $detail;
        $data['total_user_num'] = $total_user;
        $data['total_clc_num'] = $total_ci;
        return json($this->cl_logic->get_response(1,'OK',$data));
    }

    /**
     * 获取学习详情
     * @return \think\response\Json
     */
    public function ajax_get_cl_detail(){
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        $maps = [];
        $maps[] = ['id','=',$this->clid];
        $detail = model('class_learn')->get_detail($maps);
        $detail['ckid'] = !empty($detail['ckid']) ? json_decode($detail['ckid'],true) : [];

        $data['detail'] = $detail;
        return json($this->cl_logic->get_response(1,'OK',$data));
    }

    /**
     * 修改学习
     * @return \think\response\Json
     */
    public function ajax_save_cl(){
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        //检查修改学习规则
        $r = $this->cl_logic->check_save_cl_rule($params);
        if(!$r['code']){
            return json($r);
        }

        $cl_data = [];
        $cl_data['active_name'] = trim($params['active_name']);
        $cl_data['ckid'] = json_encode($params['ckid'],JSON_UNESCAPED_UNICODE);
        $cl_data['ti_num'] = trim($params['ti_num']);
        $cl_data['per_ci_num'] = trim($params['per_ci_num']);
        $cl_data['per_ti_score'] = trim($params['per_ti_score']);
        $cl_data['zeng_score'] = trim($params['zeng_score']);
        $cl_data['score_type'] = $params['score_type'];
        $cl_data['start_time'] = $params['start_time'];
        $cl_data['end_time'] = $params['end_time'];
        $cl_data['dati_time'] = trim($params['dati_time']);
        $cl_data['is_repeat_ti'] = $params['is_repeat_ti'];
        $cl_data['active_rule'] = trim($params['active_rule']);
        $cl_data['active_title'] = trim($params['active_title']);
        $cl_data['active_object'] = trim($params['active_object']);
        $cl_data['active_unit'] = trim($params['active_unit']);
        $cl_data['active_time'] = trim($params['active_time']);

        $maps = [];
        $maps[] = ['id','=',$this->clid];
        $r = model('class_learn')->force()->save($cl_data,$maps);
        if($r !== false){
            return json($this->cl_logic->get_response(1,'修改成功'));
        }
        return json($this->cl_logic->get_response(0,'修改失败'));
    }

    /**
     * 获取学习统计
     * @return \think\response\Json
     */
    public function ajax_get_cl_total(){
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        //获取学习统计
        $r = $this->cl_logic->get_cl_total($params,$this->clid);

        $data['list'] = $r['data']['list'];
        $data['total_num'] = $r['data']['total_num'];
        return json($this->cl_logic->get_response(1,'OK',$data));
    }

    /**
     * 导出学习统计
     * http://127.0.0.1/mandiao-mini-api/public/admin-class_learn-export_cl_total?token=e916a6039bff505bacaf5def2bb8435f&page=0&rows=0&department=全部&username=&start_time=&end_time=
     * @return \think\response\Json
     */
    public function export_cl_total(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //获取学习统计
        $r = $this->cl_logic->get_cl_total($params,$this->clid);
        $list = $r['data']['list'];

        $row = ['部门名称','答题人','答题次数','答对题数','答错题数'];

        $export_data = [];
        foreach($list as $k => $v){
            $export_data[$k]['department'] = $v['department'];
            $export_data[$k]['username'] = $v['username'];
            $export_data[$k]['total_num'] = $v['total_num'];
            $export_data[$k]['right_num'] = $v['right_num'];
            $export_data[$k]['wrong_num'] = $v['wrong_num'];
        }
        array_unshift($export_data,$row);

        $file_name = '学习统计' . date('YmdHi') . '.xlsx';
        $r = $this->cl_logic->gen_cl_total_excel($file_name,$export_data);

        //$this->redirect(\think\facade\Request::root(true) . '/' . $file_name);
        return json($this->cl_logic->get_response(1,'导出成功'));
    }
}